package com.csun.cmny.provider.controller;

import cn.jiguang.common.utils.Preconditions;
import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcSetMeal;
import com.csun.cmny.provider.model.dto.SetMealDto;
import com.csun.cmny.provider.model.dto.SetMealQueryDto;
import com.csun.cmny.provider.model.vo.SetMealVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcSetMealService;
import com.csun.cmny.provider.validator.OmcSetMealAddValidator;
import com.csun.cmny.provider.validator.OmcSetMealEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/setMeal", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcSetMealController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcSetMealService omcSetMealService;

    /**
     * 套餐管理-添加
     *
     * @param: [setMealDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addSetMeal(@Validated(OmcSetMealAddValidator.class) @RequestBody SetMealDto setMealDto, BindingResult br) {

        Integer result = saveSetMeal(setMealDto,getLoginAuthDto());

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 套餐管理-修改
     *
     * @param: [setMealDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/edit")
    public Wrapper editService(@Validated(OmcSetMealEditValidator.class) @RequestBody SetMealDto setMealDto,BindingResult br){

        //修改
        Integer result = saveSetMeal(setMealDto,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 套餐管理-详情
     *
     * @param: [setMealId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/getById/{setMealId}")
    public Wrapper getSetMealById(@PathVariable Long setMealId){

        // 校验Id是否为空
        Preconditions.checkArgument(setMealId != null, cmnyMessageSource
                .getMessage("SetMeal.id.null", null));

        SetMealVo setMealVo = omcSetMealService.getById(setMealId);

        //return WrapMapper.ok(setMealVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),setMealVo);
    }

    /**
     * 套餐管理-分页查询
     *
     * @param: [setMealDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody SetMealQueryDto setMealQueryDto) {

        //拷贝属性值
        OmcSetMeal omcSetMealQuery = new OmcSetMeal();
        BeanUtils.copyProperties(setMealQueryDto,omcSetMealQuery);

        //放入机构Id
        omcSetMealQuery.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = omcSetMealService.queryListWithPage(omcSetMealQuery);
        //return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),pageInfo);
    }

    /**
     * 套餐管理-项目退费标准设置
     *
     * @param: [setMealDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/setReturnStandard")
    public Wrapper queryListWithPage(@RequestBody SetMealDto setMealDto) {

        //拷贝属性值
        OmcSetMeal omcSetMealQuery = new OmcSetMeal();
        BeanUtils.copyProperties(setMealDto,omcSetMealQuery);

        int ret = omcSetMealService.setReturnStandard(omcSetMealQuery);
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 套餐管理-启用/禁用
     *
     * @param: [setMealId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/changeStatus/{setMealId}")
    public Wrapper changeStatus(@PathVariable Long setMealId) {

        // 校验Id是否为空
        Preconditions.checkArgument(setMealId != null, cmnyMessageSource
                .getMessage("SetMeal.id.null", null));

        Integer result = omcSetMealService.changeStatus(setMealId);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 套餐管理-删除
     *
     * @param: [setMealId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/deleteById/{setMealId}")
    public Wrapper deleteById(@PathVariable Long setMealId) {

        // 校验Id是否为空
        Preconditions.checkArgument(setMealId != null, cmnyMessageSource
                .getMessage("SetMeal.id.null", null));

        Integer result = omcSetMealService.deleteById(setMealId);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 套餐管理-保存
     *
     * @param: [setMealDto, loginAuthDto]
     * @return: java.lang.Integer
     */
    private Integer saveSetMeal(SetMealDto setMealDto, LoginAuthDto loginAuthDto) {
        OmcSetMeal omcSetMeal = new OmcSetMeal();
        BeanUtils.copyProperties(setMealDto,omcSetMeal);
        return omcSetMealService.saveSetMeal(omcSetMeal,loginAuthDto);
    }

}
